/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.painting;

import android.media.MediaPlayer;
import android.webkit.WebView;
import android.widget.VideoView;

import com.hercules.android.receiver.AndroidReceiver;
import com.hercules.android.receiver.ApplicationData;
import com.hercules.android.receiver.model.objects.XREImage;
import com.hercules.android.receiver.painting.views.XREViewPainter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * This class is a utility class. It is used to perform initialization and cleanup the painting context. Moreover, it
 * has a convenient method to retrieve a map of XREObjectpainters from their expected XRE class name to the painter
 * instance.
 * </p>
 * <p>
 * <b> Thread safety: </b> This class is thread safe since it is immutable.
 * </p>
 *
 * @author linwe, EveningSun
 * @version 1.0
 */
public class XREObjectPainterUtils {
    /**
     * <p>
     * Empty do-nothing constructor.
     * </p>
     */
    private XREObjectPainterUtils() {
        // empty
    }

    /**
     * <p>
     * Initialize the painting context. This method will initialize the extra painter data map in application data.
     * </p>
     *
     * @param receiver the AndroidReceiver instance
     * @throws IllegalArgumentException if the receiver is null
     */
    public static void initialize(AndroidReceiver receiver) {
        PaintersUtil.checkNull(receiver, "receiver");

        ApplicationData applicationData = receiver.getApplicationData();
        if (applicationData == null) {
            applicationData = new ApplicationData();
            receiver.setApplicationData(new ApplicationData());
        }
        if (applicationData.getExtraPainterData() == null) {
            applicationData.setExtraPainterData(new HashMap<String, Object>());
        }

        Map<String, Object> extraPainterData = applicationData.getExtraPainterData();
        extraPainterData.put("pendingImages", Collections.synchronizedList(new ArrayList<XREImage>()));
        extraPainterData.put("mediaPlayers", Collections.synchronizedMap(new HashMap<Integer, MediaPlayer>()));
        extraPainterData.put("videoViews", Collections.synchronizedMap(new HashMap<Integer, VideoView>()));
        extraPainterData.put("webViews", Collections.synchronizedMap(new HashMap<Integer, WebView>()));
        extraPainterData.put("blinkingCursorThreads", Collections.synchronizedMap(new HashMap<Integer, Thread>()));
    }

    /**
     * <p>
     * Destroy the painting context.
     * </p>
     *
     * @param receiver the AndroidReceiver instance
     * @throws IllegalArgumentException if the receiver is null
     */
    public static void finalize(AndroidReceiver receiver) {
        PaintersUtil.checkNull(receiver, "receiver");

        ApplicationData applicationData = receiver.getApplicationData();
        if (applicationData == null) {
            return;
        }

        Map<String, Object> extraPainterData = applicationData.getExtraPainterData();
        if (extraPainterData == null) {
            return;
        }
    	
    	try {
			Map<Integer, MediaPlayer> mediaPlayers = PaintersUtil.getPainterDataMap(receiver, "mediaPlayers");
			
			for (MediaPlayer mediaPlayer : mediaPlayers.values()) {
				mediaPlayer.stop();
				mediaPlayer.release();
			}
		} catch (XREObjectPainterException e) {
			e.printStackTrace();
		}
    	
        extraPainterData.remove("pendingImages");
        extraPainterData.remove("mediaPlayers");
        extraPainterData.remove("videoViews");
        extraPainterData.remove("webViews");
        extraPainterData.remove("blinkingCursorThreads");
    }

    /**
     * <p>
     * Get the default painter map from the XRE class name to painter.
     * </p>
     *
     * @return the default painter map
     */
    public static Map<String, XREObjectPainter> getDefaultPaintersMap() {
        Map<String, XREObjectPainter> result = new HashMap<String, XREObjectPainter>();

        result.put("XREApplication", new XREViewPainter());
        result.put("XREView", new XREViewPainter());
        result.put("XRERectangle", new XREViewPainter());
        result.put("XREText", new XREViewPainter());
        result.put("XREHTMLText", new XREViewPainter());
        result.put("XRESound", new XREViewPainter());
        result.put("XREVideo", new XREViewPainter());
        result.put("XREImage", new XREViewPainter());
        result.put("XRENineSliceImage", new XREViewPainter());
        result.put("XREFlash", new XREViewPainter());
        result.put("XRETextInput", new XREViewPainter());
        result.put("XREHTMLView", new XREViewPainter());

        return result;
    }
}
